package com.pwt.service;

import com.pwt.common.CustomException;
import com.pwt.common.ResultCode;
import com.pwt.common.ResultPage;
import com.pwt.entity.ProductAttribute;
import com.pwt.entity.ProductAttribute;
import com.pwt.repository.ProductAttributeRepository;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

/**
 * ProductAttribute service层
 *
 * @author auto generated
 * @date 2021-08-31 12:03:28
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ProductAttributeService {

    private ProductAttributeRepository productAttributeRepository;

    public ProductAttributeService(ProductAttributeRepository productAttributeRepository) {
        this.productAttributeRepository = productAttributeRepository;
    }

    public List<ProductAttribute> getList(String name){
//        Integer offset,Integer limit
        Specification<ProductAttribute> spec = new Specification<ProductAttribute>() {
            @Override
            public Predicate toPredicate(Root<ProductAttribute> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> list = new ArrayList<>();
                if(StringUtils.isNotBlank(name)){
                    Predicate predicate= criteriaBuilder.like(root.get("name").as(String.class),name+"%");
                    list.add(predicate);
                }
                Predicate[] p = new Predicate[list.size()];
                return criteriaBuilder.and(list.toArray(p));
            }
        };
//        Pageable pageable = PageRequest.of(offset-1,limit, Sort.Direction.DESC,"createTime");
        List<ProductAttribute> all = this.productAttributeRepository.findAll(spec,Sort.by("createTime").descending());
        return all;
    }


    @Transactional(rollbackFor = Exception.class)
    public ProductAttribute add(ProductAttribute productAttribute) throws CustomException {
        ProductAttribute byName = this.productAttributeRepository.getByType(productAttribute.getType());
        if(byName != null){
            throw new CustomException(ResultCode.DATA_EXISTED);
        }
        productAttribute = this.productAttributeRepository.save(productAttribute);
        if(productAttribute == null){
            throw new CustomException("添加失败！",new RuntimeException());
        }
        return productAttribute;
    }



    @Transactional(rollbackFor = Exception.class)
    public ProductAttribute update(ProductAttribute productAttributeModel) throws CustomException {
        ProductAttribute byType = this.productAttributeRepository.getByType(productAttributeModel.getType());
        if( byType != null && !byType.getId().equals(productAttributeModel.getId())){
            throw new CustomException(ResultCode.DATA_EXISTED);
        }
        ProductAttribute productAttribute = this.productAttributeRepository.getById(productAttributeModel.getId());
        productAttribute.setType(productAttributeModel.getType());
        productAttribute.setColor(productAttributeModel.getColor());
        productAttribute = this.productAttributeRepository.save(productAttribute);
        if(productAttribute == null){
            throw new CustomException("添加失败！",new RuntimeException());
        }
        return productAttribute;
    }

    @Transactional(rollbackFor = Exception.class)
    public void delete(String id){
        this.productAttributeRepository.deleteById(id);
    }
}
